﻿//-----------------------------------------------------------------------
// <copyright file="NewWriteupHelper.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.DomainObjects.XmlFeedObjects
{
    using System;
    using System.Linq;
    using System.Xml.Linq;

    using ChatBox.DomainObjects.ResultData;

    /// <summary>
    /// Helper methods for new writeups
    /// </summary>
    public static class NewWriteupHelper
    {
        /// <summary>
        /// read editor cools from an xml document
        /// </summary>
        /// <param name="doc">the xml document</param>
        /// <returns>the new writeups</returns>
        public static NewWriteupsDataResult ToNewWriteups(this XContainer doc)
        {
            NewWriteupsDataResult result = new NewWriteupsDataResult(true, string.Empty, null);

            var coolWriteups = doc.Element("newwriteups");
            var nodeList = coolWriteups.Elements("wu");

            var writeupList =
                from node in nodeList
                select new NewWriteup
                   {
                       WriteupType = node.Attribute("wrtype").ToWriteupType(),
                       Writeup = node.Element("e2link").ToLink(),
                       Author = node.Element("author").Element("e2link").ToLink(),
                       Parent = node.Element("parent").Element("e2link").ToLink()
                   };

            result.NewWriteups = writeupList.ToList();
            return result;
        }

        /// <summary>
        /// Convert an xml attribute to a writeup type
        /// </summary>
        /// <param name="attribute">the XML attribute to convert</param>
        /// <returns>the writeup type</returns>
        private static WriteupType ToWriteupType(this XAttribute attribute)
        {
            if (attribute == null)
            {
                return WriteupType.Unknown;
            }

            string value = attribute.Value;

            if (string.IsNullOrEmpty(value))
            {
                return WriteupType.Unknown;
            }

            // the text differs from the enum in this case becuase the e2 version includes a dash "-"
            if (value == "how-to")
            {
                return WriteupType.HowTo;
            }

            return (WriteupType)Enum.Parse(typeof(WriteupType), value, true);
        }
    }
}
